package product

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/product"
	"gitcode.com/G-YT/iHealth25/server/model/product/request"
	"gitcode.com/G-YT/iHealth25/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type ProductApi struct{}

var productService = service.ServiceGroupApp.ProductServiceGroup.ProductService

// CreateProduct 创建商品管理
// @Tags Product
// @Summary 创建商品管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body model.Product true "创建商品管理"
// @Success 200 {object} response.Response{msg=string} "创建成功"
// @Router /product/createProduct [post]
func (p *ProductApi) CreateProduct(c *gin.Context) {
	var info product.Product
	err := c.ShouldBindJSON(&info)
	if err != nil {
		global.GVA_LOG.Error("JSON绑定失败!", zap.Error(err))
		global.GVA_LOG.Error("请求数据:", zap.Any("request", c.Request.Body))
		response.FailWithMessage("数据绑定失败: "+err.Error(), c)
		return
	}
	// 验证ProductType字段
	if info.ProductType == 0 {
		response.FailWithMessage("商品类型不能为空", c)
		return
	}
	err = productService.CreateProduct(&info)
	if err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteProduct 删除商品管理
// @Tags Product
// @Summary 删除商品管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body model.Product true "删除商品管理"
// @Success 200 {object} response.Response{msg=string} "删除成功"
// @Router /product/deleteProduct [delete]
func (p *ProductApi) DeleteProduct(c *gin.Context) {
	ID := c.Query("ID")
	err := productService.DeleteProduct(ID)
	if err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage("删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteProductByIds 批量删除商品管理
// @Tags Product
// @Summary 批量删除商品管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Success 200 {object} response.Response{msg=string} "批量删除成功"
// @Router /product/deleteProductByIds [delete]
func (p *ProductApi) DeleteProductByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	err := productService.DeleteProductByIds(IDs)
	if err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("批量删除成功", c)
}

// UpdateProduct 更新商品管理
// @Tags Product
// @Summary 更新商品管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data body model.Product true "更新商品管理"
// @Success 200 {object} response.Response{msg=string} "更新成功"
// @Router /product/updateProduct [put]
func (p *ProductApi) UpdateProduct(c *gin.Context) {
	var info product.Product
	err := c.ShouldBindJSON(&info)
	if err != nil {
		global.GVA_LOG.Error("JSON绑定失败!", zap.Error(err))
		global.GVA_LOG.Error("请求数据:", zap.Any("request", c.Request.Body))
		response.FailWithMessage("数据绑定失败: "+err.Error(), c)
		return
	}
	// 验证ProductType字段
	if info.ProductType == 0 {
		response.FailWithMessage("商品类型不能为空", c)
		return
	}
	err = productService.UpdateProduct(info)
	if err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败:"+err.Error(), c)
		return
	}
	response.OkWithMessage("更新成功", c)
}

// FindProduct 用id查询商品管理
// @Tags Product
// @Summary 用id查询商品管理
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "用id查询商品管理"
// @Success 200 {object} response.Response{data=model.Product,msg=string} "查询成功"
// @Router /product/findProduct [get]
func (p *ProductApi) FindProduct(c *gin.Context) {
	ID := c.Query("ID")
	reProduct, err := productService.GetProduct(ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败:"+err.Error(), c)
		return
	}
	response.OkWithData(reProduct, c)
}

// GetProductList 分页获取商品管理列表
// @Tags Product
// @Summary 分页获取商品管理列表
// @Security ApiKeyAuth
// @Accept application/json
// @Produce application/json
// @Param data query request.ProductSearch true "分页获取商品管理列表"
// @Success 200 {object} response.Response{data=response.PageResult,msg=string} "获取成功"
// @Router /product/getProductList [get]
func (p *ProductApi) GetProductList(c *gin.Context) {
	var pageInfo request.ProductSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	list, total, err := productService.GetProductInfoList(pageInfo)
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败:"+err.Error(), c)
		return
	}
	response.OkWithDetailed(response.PageResult{
		List:     list,
		Total:    total,
		Page:     pageInfo.Page,
		PageSize: pageInfo.PageSize,
	}, "获取成功", c)
}

// GetProductDetail 获取商品详情（无鉴权）
// @Tags Product
// @Summary 获取商品详情（无鉴权）
// @Accept application/json
// @Produce application/json
// @Param ID query uint true "商品ID"
// @Success 200 {object} response.Response{data=model.Product,msg=string} "获取成功"
// @Router /product/detail [get]
func (p *ProductApi) GetProductDetail(c *gin.Context) {
	ID := c.Query("ID")
	if ID == "" {
		response.FailWithMessage("商品ID不能为空", c)
		return
	}
	reProduct, err := productService.GetProductDetail(ID)
	if err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("商品不存在或已下架", c)
		return
	}
	response.OkWithData(reProduct, c)
}
